@extends('layouts.default')

@section('title')
	小区管理
@stop

@section('pre')
	<li class="active"><span>@yield('title')</span></li>
@stop

@section('css')
@stop

@section('content')

<div class="row">
	<div class="col-lg-12">
		<div class="main-box">
			<header class="main-box-header clearfix">
			@if( isset( $groupList ) && !empty( $groupList ) )
			<h2>当前小区</h2>
			</header>
			<div class="main-box-body clearfix">
		
				<div class="col-lg-12">
					<form role="form" id="groupPost">
						<div class="form-group form-group-select2">
							<label>请选择一个小区</label>
							<select style="width:300px" id="sel2" name="group">
								<option value="0">选择一个小区</option>
									
									@foreach( $groupList as $k => $val )

										<option value="{{ $k }}">{{ $val }}</option>

									@endforeach

							</select>
						</div>
					</form>
				</div>
			</div>
			@else
			<h2>您当前还没有小区,<a href="{{ url('groupCreate') }}">点此</a>创建一个</h2>
			</header>	
			@endif
		</div>
	</div>
</div>


	<div class="col-lg-12 hidden" id="show-group-info">
		
		<div class="main-box">
			<header class="main-box-header clearfix">
				<h2>小区设置</h2>
			</header>
			<form role="form" method="post" action="{{ url('groupSetting') }}">
				<div class="main-box-body clearfix">
					
					<input type="hidden" id="group-id" name="id" value="">

					<div class="form-group col-xs-6">
						<label for="exampleTextarea">小区名称</label>
						<input class="form-control" id="group-name" name="name" type="text" placeholder="请输入小区名称" value="">
					</div>

					<div class="form-group col-lg-12" id="loctown">
						<label for="exampleInputFile">县区</label>
						<div class="form-group-select2">
							<select id="loc_town" name="location" style="width:120px">
							</select>
						</div>
					</div>

					<div class="col-lg-8">
						<label for="exampleTextarea">小区描述</label>
						<textarea class="form-control" id="group-about" name="about" rows="3" placeholder="请输入小区描述信息"></textarea>
					</div>
					
					<div class="col-lg-12" style="text-align: center;">
						<br/>
						<input type="submit" class="btn btn-primary" name="edit" value="修改">
					</div>
				</div>

			</form>
		</div>
	</div>


@stop

@section('js')

<script type="text/javascript">

	jQuery(document).ready(function($) {
		
		$select = $('#sel2');
		$select.select2();


		var uri = "{{ url('groupSetting') }}";

		$select.on('change', function(){

			if(!$('#show-group-info').hasClass('hidden'))
			{
				$('#show-group-info').fadeOut().addClass('hidden');
			}

			if($("#groupPost").serialize() == 'group=0')
			{
				return;
			}

			$('#show-group-info').before('<div id="show-group-loading" style="position:absolute;left:50%;top:80%;font-size:20px;margin:150px -30px"><i class="fa-li fa fa-spinner fa-spin"></i>加载中...</div>');

			$.post(uri, $("#groupPost").serialize(), function(data){
				
					$('#group-id').val(data[0]['id']);
					$('#group-name').val(data[0]['name']);
					$('#group-about').val(data[0]['about']);

					$('#loc_town').select2();
					$('#loc_town').val(data[0]['location']).trigger("change");

					$('#show-group-info').fadeIn().removeClass('hidden');
					$('#show-group-loading').remove();

			});

		});

	});
</script>

@stop